package icu.sunway.gushenBlogBackEnd.Controller;

import icu.sunway.gushenBlogBackEnd.Entity.Data.ArticleDO;
import icu.sunway.gushenBlogBackEnd.Entity.View.Article.ArticlePostVO;
import icu.sunway.gushenBlogBackEnd.Entity.View.Article.ArticleUpdateVO;
import icu.sunway.gushenBlogBackEnd.Entity.View.ResultVO;
import icu.sunway.gushenBlogBackEnd.Service.ArticleService;
import icu.sunway.gushenBlogBackEnd.Service.UserService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/article")
public class ArticleController {

    final
    ArticleService articlesService;
    UserService userService;

    public ArticleController(ArticleService articlesService,
                             UserService userService) {
        this.articlesService = articlesService;
        this.userService = userService;
    }

    @PostMapping("/add")
    public ResultVO<Boolean> addArticles(@RequestBody ArticlePostVO articlePostVO,
                                         @RequestHeader String token) {
        return articlesService.addArticle(articlePostVO, token);
    }

    @GetMapping("/list")
    public ResultVO<List<ArticleDO>> getArticleList() {
        return articlesService.getArticleList();
    }

    @GetMapping("/list_page")
    public ResultVO<List<ArticleDO>> getArticleListByPage(@RequestParam int curPage,
                                                          @RequestParam int pageSize) {
        return articlesService.getArticleListByPage(curPage, pageSize);
    }

    @GetMapping("/count")
    public ResultVO<Long> getArticleCount() {
        return articlesService.getArticleCount();
    }

    @GetMapping("/view")
    public ResultVO<ArticleDO> getArticleById(@RequestParam String id) {
        return articlesService.getArticleById(id);
    }

    @PutMapping("/update")
    public ResultVO<Boolean> updateArticle(@RequestBody ArticleUpdateVO articleUpdateVO) {
        return articlesService.updateArticle(articleUpdateVO);
    }

    @DeleteMapping("/delete")
    public ResultVO<Boolean> deleteArticle(@RequestParam String id) {
        return articlesService.deleteArticle(id);
    }
}
